package com.vader.admin.controller;

import com.vader.admin.entity.DO.BaseGroupUserRelation;
import com.vader.admin.entity.DO.BaseUserInfo;
import com.vader.admin.entity.Query.BaseUserInfoQuery;
import com.vader.admin.entity.Query.EditIntegrationQuery;
import com.vader.admin.entity.Query.JoinGroupQuery;
import com.vader.admin.entity.VO.BaseUserIntegrationVO;
import com.vader.admin.service.BaseGroupUserRelationService;
import com.vader.admin.service.BaseIntegrationService;
import com.vader.admin.service.BaseUserInfoService;
import com.vader.common.entity.CommonResponse;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 用户信息表(BaseUserInfo)表控制层
 *
 * @author Vader
 * @since 2021-10-06 12:46:39
 */
@RestController
@RequestMapping("/baseUserInfo")
public class BaseUserInfoController {
    /**
     * 服务对象
     */
    @Resource
    private BaseUserInfoService baseUserInfoService;

    @Resource
    private BaseIntegrationService baseUserIntegrationService;

    @Resource
    private BaseGroupUserRelationService baseGroupUserRelationService;

    /**
     * 分页查询
     *
     * @param baseUserInfo 筛选条件
     * @param pageRequest  分页对象
     * @return 查询结果
     */
    @GetMapping
    public CommonResponse<Page<BaseUserInfo>> queryByPage(@RequestBody BaseUserInfo baseUserInfo, PageRequest pageRequest) {
        return CommonResponse.success(this.baseUserInfoService.queryByPage(baseUserInfo, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryUserById/{id}")
    public CommonResponse<BaseUserIntegrationVO> queryById(@PathVariable("id") Long id) {
        return CommonResponse.success(this.baseUserInfoService.queryById(id));
    }

    /**
     * 新增用户
     *
     * @param baseUserInfoQuery 实体
     * @return 新增结果
     */
    @PostMapping("/addUser")
    public CommonResponse<BaseUserInfo> addUser(@RequestBody @Valid BaseUserInfoQuery baseUserInfoQuery) {
        BaseUserInfo baseUserInfo = BaseUserInfo.from(baseUserInfoQuery);
        return CommonResponse.success(baseUserInfoService.insert(baseUserInfo));
    }

    /**
     * 用户信息修改
     *
     * @param baseUserInfoQuery 实体
     * @return 编辑结果
     */
    @PutMapping("/editUser")
    public CommonResponse<BaseUserInfo> editUser(@RequestBody @Valid BaseUserInfoQuery baseUserInfoQuery) {
        BaseUserInfo baseUserInfo = BaseUserInfo.from(baseUserInfoQuery);
        return CommonResponse.success(this.baseUserInfoService.update(baseUserInfo));
    }

    /**
     * 用户注销
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/deleteUser/{id}")
    public CommonResponse<Boolean> deleteById(@PathVariable("id") Long id) {
        return CommonResponse.success(this.baseUserInfoService.deleteById(id));
    }

    /**
     * 积分变更
     *
     * @param editIntegrationQuery 变更详情
     * @return 变更结果
     */
    @PostMapping("/editIntegration")
    public CommonResponse<Boolean> editIntegration(@RequestBody @Valid EditIntegrationQuery editIntegrationQuery) {
        return CommonResponse.success(baseUserIntegrationService.update(editIntegrationQuery));
    }

    /**
     * 加入组织
     * @param joinGroupQuery 加入组织记录
     * @return 加入结果
     */
    @PostMapping("/joinGroup")
    public CommonResponse<Boolean> joinGroup(@RequestBody @Valid JoinGroupQuery joinGroupQuery) {
        BaseGroupUserRelation baseGroupUserRelation = BaseGroupUserRelation.from(joinGroupQuery);
        return CommonResponse.success(baseGroupUserRelationService.insert(baseGroupUserRelation));
    }
}

